﻿#region

using System;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Tank90.Properties;
using Timer = System.Windows.Forms.Timer;

#endregion

namespace Tank90
{
    public partial class MainStar : Form
    {
        private readonly Label label1 = new Label();
        private readonly LoadMap loadMap = new LoadMap();
        private readonly Timer timer = new Timer();
        private int Enemycount = 30;
        private bool IsLoadMap;
        private Enemy enemy;
        private PlayerTank player;
        private int playerLife = 3;

        public MainStar()
        {
            InitializeComponent();
        }

        private void gamePanel1_Paint(object sender, PaintEventArgs e)
        {
            if (!IsLoadMap)
            {
                IsLoadMap = true;
            }
      
            MainManager.mainManager.GetStar(e.Graphics, out Enemycount, out playerLife);
        }

        private void MainStar_Load(object sender, EventArgs e)
        {
             
            Sound.Play("start.wav");
            label1.Location = new Point(60, 510);
            gamePanel2.Controls.Add(label1);
            label1.Font = new Font("", 18);

            var dirinfo = new DirectoryInfo(Application.StartupPath);
            string filePath = dirinfo.Parent.Parent.FullName + "\\MapData\\MyMap.xlsx";
            MainManager.mainManager.setMap(loadMap.ReadMaps(filePath));
            timer.Interval = 30;
            timer.Tick += timer_Tick;
            timer.Start();
            //Thread moveThread = new Thread(new ThreadStart(PlayMove));
            //moveThread.Start();
        }

        //private void PlayMove()
        //{

        //    while (true)
        //    {
        //        Thread.Sleep(400);
        //        Sound.Play("playerMove.wav");
        //    }
           
    
            
        //}

        private void timer_Tick(object sender, EventArgs e)
        {
           
            gamePanel1.Invalidate();
            gamePanel2.Invalidate();
            //MainManager.mainManager.CreateEnemyThread();
        }

        private void MainStar_KeyDown(object sender, KeyEventArgs e)
        {
            MainManager.mainManager.Move(e);
        }

        private void MainStar_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void gamePanel2_Paint(object sender, PaintEventArgs e)
        {
            for (int i = 0; i < Enemycount; i++)
            {
                e.Graphics.DrawImage(Resources.Enemy1Down, new Point(45*(i%3), -30 + 45*((i/3) + 1)));
            }

            e.Graphics.DrawImage(Resources.Player1Up, new Point(20, 500));
            label1.Text = "*" + playerLife;
        }
    }
}